package com.netty.demo.echo;

import com.netty.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhangtao
 * @create 2021/11/2 9:41
 */
public class MessageDecoder extends ByteToMessageDecoder {
    private byte[] plcHead1 = new byte[]{3, 0, 0, 22, 17, -32, 0, 0, 0, 1, 0, -64, 1, 10, -63, 2, 1, 2, -62, 2, 1, 0};
    private byte[] plcHead2 = new byte[]{3, 0, 0, 25, 2, -16, -128, 50, 1, 0, 0, 4, 0, 0, 8, 0, 0, -16, 0, 0, 1, 0, 1, 1, -32};

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        StringBuffer request = new StringBuffer();
        StringBuffer requestb = new StringBuffer();
        List<Byte> byteList = new ArrayList<>();
        int i = byteBuf.readableBytes();
        int j = 0;
        while (i > 0) {
            i--;
            byte b = byteBuf.readByte();
            requestb.append(b);
            requestb.append(",");
            request.append(j);
            request.append(",");
            byteList.add(b);
            j++;
        }
        System.out.println(request);
        System.out.println(requestb);
//        list.add(request);
//        list.add(requestb);
        list.add(byteList);
    }

}
